package com.jk.vehicle.mini.controller;

import com.alibaba.excel.util.CollectionUtils;
import com.jk.vehicle.mini.constant.UserTypeEnum;
import com.jk.vehicle.mini.entity.User;
import com.jk.vehicle.mini.entity.UserBicycleLink;
import com.jk.vehicle.mini.service.CorpService;
import com.jk.vehicle.mini.service.MileageRecordService;
import com.jk.vehicle.mini.service.UserBicycleLinkService;
import com.jk.vehicle.mini.service.UserService;
import com.jk.vehicle.mini.vo.StatisticsVO;
import com.jk.vehicle.mini.vo.TotalStatisticsVO;
import com.jk.vehicle.core.controller.BaseController;
import com.jk.vehicle.core.entity.ResponseEntity;
import com.jk.vehicle.core.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static java.time.temporal.ChronoUnit.DAYS;

@Api(value = "里程相关接口", tags = "里程相关接口")
@Slf4j
@RestController
@RequestMapping("/milleage")
public class MileageController extends BaseController {
    @Resource
    private MileageRecordService mileageRecordService;

    @Resource
    private UserBicycleLinkService userBicycleLinkService;

    @Resource
    private CorpService corpService;

    @Resource
    private UserService userService;

    /**
     * 累计里程和陪伴天数进行统计√
     */
    @ApiOperation("累计里程和陪伴天数统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bicycleId", value = "车辆主键ID"),
            @ApiImplicitParam(name = "userId", value = "当前用户ID"),
            @ApiImplicitParam(name = "corpId", value = "厂家ID")
    })
    @GetMapping("/total_statistics")
    public ResponseEntity<TotalStatisticsVO> totalStatistics(Long userId, Long bicycleId, Long corpId){
        if (corpId == null || corpId.longValue() == 0) {
            return failResult("厂家id不能为空！");
        }
        if (corpId == -1) {
            return failResult("厂家id不能等于-1！");
        }
        String dsName = corpService.selectDsNameById(corpId);
        if (bicycleId == null){
            return failResult("缺少必要参数！");
        }
        User user = userService.findById(userId, null);
        if (user == null) {
            return failResult("该用户不存在！");
        }
        // 查询车辆总陪伴天数
        Condition condition = new Condition(UserBicycleLink.class);
        condition.createCriteria().andEqualTo("userId", userId)
                .andEqualTo("bicycleId", bicycleId)
                .andEqualTo("userType", UserTypeEnum.MANAGER.getCode());
        List<UserBicycleLink> userBicycleLinkList = userBicycleLinkService.findByCondition(condition, dsName);
        if (CollectionUtils.isEmpty(userBicycleLinkList)){
            return failResult("待统计车辆信息关联信息不存在！");
        }
        UserBicycleLink userBicycleLink = userBicycleLinkList.get(0);
        // 计算售出时间到今日的天数
        long days = DAYS.between(TimeUtil.date2LocalDate(userBicycleLink.getBuyDate()), TimeUtil.date2LocalDate(new Date()));
        // 查询车辆总里程数据
        BigDecimal totalMileage = mileageRecordService.findTotalMileageOfBicycle(bicycleId, dsName);
        // 封装数据并返回
        TotalStatisticsVO totalStatisticsVO = new TotalStatisticsVO(totalMileage, days);
        return successResult(totalStatisticsVO);
    }

    /**
     * 查询特定车辆当日行程统计√
     * @param bicycleId 车辆id
     * @return 结果
     */
    @ApiOperation("查询特定车辆当日行程统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bicycleId", value = "车辆ID"),
            @ApiImplicitParam(name = "corpId", value = "厂家ID")
    })
    @ApiImplicitParam(name = "bicycleId", value = "车辆ID")
    @GetMapping("/bicycle_today_statistics")
    public ResponseEntity<StatisticsVO> bicycleTodayStatistics(Long bicycleId, Long corpId){
        if (corpId == null || corpId.longValue() == 0) {
            return failResult("厂家id不能为空！");
        }
        if (corpId == -1) {
            return failResult("厂家id不能等于-1！");
        }
        String dsName = corpService.selectDsNameById(corpId);
        String someDay = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        return successResult(mileageRecordService.queryMileageByDay(someDay, someDay, bicycleId, dsName));
    }
}
